package com.base.library.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 万能适配器
 */

public class MultiTypeAdapter extends RecyclerView.Adapter<MultiTypeAdapter.ItemViewHolder> {

    // item点击事件
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;
    private OnViewChange onViewChange;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnViewChange(OnViewChange onViewChange) {
        this.onViewChange = onViewChange;
    }

    public interface IItem {
        /**
         * 得到item对应的布局文件
         *
         * @return
         */
        int getLayout();

        /**
         * 得到item对应的VariableId
         *
         * @return
         */
        int getVariableId();

        void onBinding(ViewDataBinding binding);

    }

    public interface OnViewChange {
        void onViewAttachedToWindow(ItemViewHolder holder);

        void onViewDetachedFromWindow(ItemViewHolder holder);
    }

    // item集合
    private List<IItem> items = new ArrayList<>();

    public List<IItem> getItems() {
        return items;
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getLayout();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ItemViewHolder.create(parent, viewType);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        items.get(position).onBinding(holder.getBinding());
        holder.bindTo(items.get(position));

    }

    @Override
    public void onViewAttachedToWindow(ItemViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (onViewChange != null) {
            onViewChange.onViewAttachedToWindow(holder);
        }
    }

    @Override
    public void onViewDetachedFromWindow(ItemViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (onViewChange != null) {
            onViewChange.onViewDetachedFromWindow(holder);
        }
    }

    /**
     * 统一的viewholder
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ViewDataBinding binding;
        private IItem iItem;

        /**
         * 构建viewholder
         *
         * @param parent
         * @param viewType
         * @return
         */
        public static ItemViewHolder create(ViewGroup parent, int viewType) {
            ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false);
            return new ItemViewHolder(binding);
        }

        public ItemViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        /**
         * 绑定数据
         *
         * @param item
         */
        public void bindTo(IItem item) {
            this.iItem = iItem;
            binding.setVariable(item.getVariableId(), item);
            binding.executePendingBindings();
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public IItem getItem() {
            return iItem;
        }
    }

    public void setItem(IItem item) {
        clearItems();
        addItem(item);
    }

    public void setItems(List<IItem> items) {
        clearItems();
        addItems(items);
    }

    public void addItem(IItem item) {
        items.add(item);
    }

    public void addItem(IItem item, int index) {
        items.add(index, item);
    }

    public IItem getItem(int position) {
        return items.get(position);
    }

    public void setItem(IItem item, int index) {
        items.set(index, item);
    }

    public void addItems(List<IItem> items) {
        this.items.addAll(items);
    }

    public int removeItem(IItem item) {
        int pos = findPos(item);
        items.remove(item);
        return pos;
    }

    public void removeItem(int index) {
        items.remove(index);
    }

    public void clearItems() {
        items.clear();
    }

    public int findPos(IItem item) {
        return items.indexOf(item);
    }
}
